package com.ruoyi.mateTemplate.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.finishedProduct.domain.FinishedProduct;
import com.ruoyi.finishedProduct.domain.FinishedProductModel;
import com.ruoyi.finishedProduct.mapper.FinishedProductMapper;
import com.ruoyi.mateCheck.domain.MateUserDevice;
import com.ruoyi.mateDevice.domain.MateDevice;
import com.ruoyi.mateDevice.mapper.MateDeviceMapper;
import com.ruoyi.mateRequistion.domain.MateRequistion;
import com.ruoyi.mateRequistion.domain.MateRequistionModel;
import com.ruoyi.mateRequistion.mapper.MateRequistionMapper;
import com.ruoyi.mateTemplate.domain.MateTemplate;
import com.ruoyi.mateTemplate.domain.MateTemplateModel;
import com.ruoyi.mateTemplate.domain.ProductTemplate;
import com.ruoyi.mateTemplate.domain.ProductTemplateModel;
import com.ruoyi.mateTemplate.mapper.TemplateMapper;
import com.ruoyi.mateTemplate.service.MateTemplateService;
import com.ruoyi.mateThrough.domain.MateThrough;
import com.ruoyi.mateThrough.mapper.MateThroughMapper;
import com.ruoyi.mateWarehousing.domain.MateWarehousing;
import com.ruoyi.mateWarehousing.mapper.MateWarehousingMapper;
import com.ruoyi.mateproduction.domain.MateAsepsis;
import com.ruoyi.mateproduction.domain.MateAsepsisCultivate;
import com.ruoyi.mateproduction.domain.MateAsepsisResult;
import com.ruoyi.mateproduction.domain.MateCleanRecord;
import com.ruoyi.mateproduction.domain.MateCleanRecordModel;
import com.ruoyi.mateproduction.domain.MateDaily;
import com.ruoyi.mateproduction.domain.MateDailyModel;
import com.ruoyi.mateproduction.domain.MateEthylene;
import com.ruoyi.mateproduction.domain.MateProductionPlan;
import com.ruoyi.mateproduction.domain.MateProductionPlanModel;
import com.ruoyi.mateproduction.domain.MateProductionRecord;
import com.ruoyi.mateproduction.domain.MateProductionRecordModel;
import com.ruoyi.mateproduction.domain.MateRecordInformation;
import com.ruoyi.mateproduction.domain.MateTransfer;
import com.ruoyi.mateproduction.domain.MateTransferInformation;
import com.ruoyi.mateproduction.domain.MateTransferModel;
import com.ruoyi.mateproduction.domain.MtCourseCheck;
import com.ruoyi.mateproduction.domain.MtCourseProduct;
import com.ruoyi.mateproduction.domain.MtCourseProject;
import com.ruoyi.mateproduction.domain.MtCourseSample;
import com.ruoyi.mateproduction.domain.ReleaseReview;
import com.ruoyi.mateproduction.mapper.MateAsepsisMapper;
import com.ruoyi.mateproduction.mapper.MateCleanRecordMapper;
import com.ruoyi.mateproduction.mapper.MateCourseMapper;
import com.ruoyi.mateproduction.mapper.MateDailyMapper;
import com.ruoyi.mateproduction.mapper.MateEthyleneMapper;
import com.ruoyi.mateproduction.mapper.MateTransferMapper;
import com.ruoyi.mateproduction.mapper.MtProductionMapper;
import com.ruoyi.mateproduction.mapper.MtRecordMapper;
import com.ruoyi.material.domain.MateProductDevice;
import com.ruoyi.material.mapper.MateProductMapper;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import com.ruoyi.product.domain.Product;
import com.ruoyi.product.domain.ProductRelation;
import com.ruoyi.product.mapper.ProductMapper;
import com.ruoyi.productCheck.domain.CheckInformation;
import com.ruoyi.productCheck.domain.CheckInformationModel;
import com.ruoyi.productCheck.domain.CheckSample;
import com.ruoyi.productCheck.domain.ProductReport;
import com.ruoyi.productCheck.domain.ProductReportModel;
import com.ruoyi.productCheck.mapper.CheckInformationMapper;
import com.ruoyi.productCheck.mapper.ProductReportMapper;
import com.ruoyi.warehousing.domain.Warehousing;
import com.ruoyi.warehousing.domain.WarehousingRecords;
import com.ruoyi.warehousing.mapper.WarehousingMapper;
import com.ruoyi.warehousing.mapper.WarehousingRecordsMapper;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateTemplateServiceImpl implements MateTemplateService {

    @Autowired
    MateRequistionMapper mateRequistionMapper;

    @Autowired
    MateTransferMapper mateTransferMapper;

    @Autowired
    MtRecordMapper mtRecordMapper;

    @Autowired
    MateDeviceMapper mateDeviceMapper;

    @Autowired
    MateCourseMapper mateCourseMapper;

    @Autowired
    MateCleanRecordMapper mateCleanRecordMapper;

    @Autowired
    MateDailyMapper mateDailyMapper;

    @Autowired
    CheckInformationMapper checkInformationMapper;

    @Autowired
    ProductReportMapper productReportMapper;

    @Autowired
    FinishedProductMapper finishedProductMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    MateProductMapper mateProductMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MateWarehousingMapper mateWarehousingMapper;

    @Autowired
    MateThroughMapper mateThroughMapper;

    @Autowired
    TemplateMapper templateMapper;

    @Autowired
    WarehousingMapper warehousingMapper;

    @Autowired
    MtProductionMapper mtProductionMapper;

    @Autowired
    MateAsepsisMapper mateAsepsisMapper;

    @Autowired
    MateEthyleneMapper mateEthyleneMapper;

    @Autowired
    private WarehousingRecordsMapper warehousingRecordsMapper;


    public static void main(String[] args) {
        // 保留两位小数
        for (int i = 0; i <= 40; i++) {
            Double minValue = 0.26;
            Double maxValue = 0.31;
            double randomValue = minValue + (maxValue - minValue) * new Random().nextDouble();
            DecimalFormat df = new DecimalFormat("#.##");
            String formattedResult = df.format(randomValue);
            System.out.println(formattedResult);
        }

    }

    @Override
    public int insertTemplate(MateTemplate mateTemplate) {
        MateTransfer mateTransfer = mateTemplate.getMateTransfer();
        Long deptId = SecurityUtils.getDeptId();
        mateTransfer.setDeptId(SecurityUtils.getDeptId());
        Long productId = mateTransfer.getProductId();

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("productId", productId);
        resMap.put("deptId", deptId);

        MateProductionRecord mateProductionRecord = mateTemplate.getMateProductionRecord();
        mateProductionRecord.setDeptId(deptId);
        mateProductionRecord.setProductId(productId);
        mateProductionRecord.setProductName(mateTransfer.getProductName());

        MtCourseCheck mtCourseCheck = mateTemplate.getMtCourseCheck();
        mtCourseCheck.setDeptId(deptId);
        mtCourseCheck.setProductId(productId);
        mtCourseCheck.setProductName(mateTransfer.getProductName());

        MateCleanRecord mateCleanRecord = mateTemplate.getMateCleanRecord();
        mateCleanRecord.setDeptId(deptId);
        mateCleanRecord.setProductId(productId);
        mateCleanRecord.setProductName(mateTransfer.getProductName());

        MateDaily mateDaily = new MateDaily();
        mateDaily.setDeptId(deptId);
        mateDaily.setProductId(mateTransfer.getProductId());

        CheckInformation checkInformation = mateTemplate.getCheckInformation();
        checkInformation.setDeptId(deptId);
        checkInformation.setProductId(productId);
        checkInformation.setProductName(mateTransfer.getProductName());

        ProductReport productReport = mateTemplate.getProductReport();
        productReport.setDeptId(deptId);
        productReport.setProductId(productId);
        productReport.setProductName(mateTransfer.getProductName());

        FinishedProduct finishedProduct = new FinishedProduct();
        finishedProduct.setDeptId(deptId);
        finishedProduct.setProductId(productId);

        List<ProductRelation> productRelations = productMapper.selectMateProductId(resMap);
        int num = 0;

        try {
            num = mateTransferMapper.addMateTransferTemplate(mateTransfer);
            if (mateTransfer.getMateTransferModels() != null) {
                for (MateTransferModel mateTransferModel : mateTransfer.getMateTransferModels()) {
                    mateTransferModel.setDeptId(SecurityUtils.getDeptId());
                    mateTransferModel.setTransferId(mateTransfer.getTransferId());
                    mateTransferModel.setProductId(productId);
                    mateTransferMapper.addMateTransferModelTemplate(mateTransferModel);
                }
            }

            mtRecordMapper.addMateRecordTemplate(mateProductionRecord);
            if (mateProductionRecord.getMateProductionRecordModels() != null) {
                for (MateProductionRecordModel mateProductionRecordModel : mateProductionRecord.getMateProductionRecordModels()) {
                    mateProductionRecordModel.setDeptId(SecurityUtils.getDeptId());
                    mateProductionRecordModel.setMtRecordId(mateProductionRecord.getMtRecordId());
                    mateProductionRecordModel.setProductId(productId);
                    mtRecordMapper.addMateRecordModelTemplate(mateProductionRecordModel);
                }
            }

            MateRecordInformation mateRecordInformation = new MateRecordInformation();
            if (productRelations != null) {
                for (ProductRelation productRelation : productRelations) {
                    mateRecordInformation.setDeptId(deptId);
                    mateRecordInformation.setMtRecordId(mateProductionRecord.getMtRecordId());
                    mateRecordInformation.setProductId(productId);
                    mateRecordInformation.setMateproductId(productRelation.getMateproductId());
                    mateRecordInformation.setMateProductName(productRelation.getMateProductName());
                    mtRecordMapper.addMateRecordInformationTemplate(mateRecordInformation);
                }
            }

            mateCourseMapper.addMateCourseTemplate(mtCourseCheck);
            MtCourseProduct mtCourseProduct = new MtCourseProduct();
            if (productRelations != null) {
                for (ProductRelation productRelation : productRelations) {
                    mtCourseProduct.setMateProductName(productRelation.getMateProductName());
                    mtCourseProduct.setMateproductId(productRelation.getMateproductId());
                    mtCourseProduct.setDeptId(deptId);
                    mtCourseProduct.setMtCourseId(mtCourseCheck.getMtCourseId());
                    mtCourseProduct.setProductId(productId);
                    mateCourseMapper.addMateCourseProductTemplate(mtCourseProduct);
                }
            }

            if (mtCourseCheck.getMtCourseProjects() != null) {
                for (MtCourseProject mtCourseProject : mtCourseCheck.getMtCourseProjects()) {
                    mtCourseProject.setDeptId(SecurityUtils.getDeptId());
                    mtCourseProject.setMtCourseId(mtCourseCheck.getMtCourseId());
                    mtCourseProject.setProductId(productId);
                    mateCourseMapper.addMateCourseProjectTemplate(mtCourseProject);
                }
            }

            mateCleanRecordMapper.addMateCleanRecordTemplate(mateCleanRecord);
            if (mateCleanRecord.getMateCleanRecordModels() != null) {
                for (MateCleanRecordModel mateCleanRecordModel : mateCleanRecord.getMateCleanRecordModels()) {
                    mateCleanRecordModel.setDeptId(SecurityUtils.getDeptId());
                    mateCleanRecordModel.setCleanId(mateCleanRecord.getCleanId());
                    mateCleanRecordModel.setProductId(productId);
                    mateCleanRecordMapper.addMateCleanRecordModelTemplate(mateCleanRecordModel);
                }
            }

            mateDailyMapper.addMateDailyTemplate(mateDaily);
            MateDailyModel mateDailyModel = new MateDailyModel();
            mateDailyModel.setDailyId(mateDaily.getDailyId());
            mateDailyModel.setDeptId(SecurityUtils.getDeptId());
            mateDailyModel.setProductId(productId);
            mateDailyModel.setProductName(mateTransfer.getProductName());
            mateDailyMapper.addMateDailyModelTemplate(mateDailyModel);

            checkInformationMapper.addInformationTemplate(checkInformation);
            if (checkInformation.getCheckInformationModel() != null) {
                for (CheckInformationModel checkInformationModel : checkInformation.getCheckInformationModel()) {
                    checkInformationModel.setDeptId(deptId);
                    checkInformationModel.setCheckId(checkInformation.getCheckId());
                    checkInformationModel.setProductId(productId);
                    checkInformationMapper.addInformationModelTemplate(checkInformationModel);
                }
            }

            productReportMapper.addReportTemplate(productReport);
            finishedProductMapper.addFinishedProductTemplate(finishedProduct);
            FinishedProductModel finishedProductModel = new FinishedProductModel();
            finishedProductModel.setDeptId(deptId);
            finishedProductModel.setFinishedProductId(finishedProduct.getFinishedProductId());
            finishedProductModel.setProductId(productId);
            finishedProductMapper.addFinishedProductModelTemplate(finishedProductModel);

        } catch (Exception e) {

            mateTransferMapper.removeMateTransferTemplate(mateTransfer.getTransferId());
            mateTransferMapper.removeMateTransferModelTemplate(mateTransfer.getTransferId());

            mtRecordMapper.removeMtRecordTemplate(mateProductionRecord.getMtRecordId());
            mtRecordMapper.removeMtRecordModelTemplate(mateProductionRecord.getMtRecordId());
            mtRecordMapper.removeMtRecordInformationTemplate(mateProductionRecord.getMtRecordId());

            mateCourseMapper.removeMateCourseTemplate(mtCourseCheck.getMtCourseId());
            mateCourseMapper.removeMateCourseProductTemplate(mtCourseCheck.getMtCourseId());
            mateCourseMapper.removeMateCourseProjectTemplate(mtCourseCheck.getMtCourseId());

            mateDailyMapper.removeMateDailyTemplate(mateDaily.getDailyId());
            mateDailyMapper.removeMateDailyModelTemplate(mateDaily.getDailyId());

            checkInformationMapper.removeCheckInformationTemplate(checkInformation.getCheckId());
            checkInformationMapper.removeCheckInformationModelTemplate(checkInformation.getCheckId());

            productReportMapper.removeReportTemplate(productReport.getReportId());

            finishedProductMapper.removeFinishedProductTemplate(finishedProduct.getFinishedProductId());
            finishedProductMapper.removeFinishedProductModelTemplate(finishedProduct.getFinishedProductId());

        }
        return num;
    }

    @Override
    @Transactional
    public void createTemplate(MateTemplate mateTemplate) {
        Long productId = mateTemplate.getProductId();
        Map<String, Object> resMap = new HashMap<>();
        Long deptId = SecurityUtils.getDeptId();
        resMap.put("productId", productId);
        resMap.put("deptId", deptId);

        MateProductionPlan mateProductionPlan = new MateProductionPlan();

        MateTransfer mateTransfer = new MateTransfer();

        MateProductionRecord mateProductionRecord = new MateProductionRecord();

        MtCourseCheck mtCourseCheck = new MtCourseCheck();

        MateCleanRecord mateCleanRecord = new MateCleanRecord();

        MateDaily mateDaily = new MateDaily();

        CheckInformation checkInformation = new CheckInformation();

        ProductReport productReport = new ProductReport();

        FinishedProduct finishedProduct = new FinishedProduct();

        MateRequistion mateRequistion = new MateRequistion();

        MateThrough mateThrough = new MateThrough();

        ProductTemplate productionTemplate = templateMapper.selectProductTemplate(resMap);

        ReleaseReview releaseReview=new ReleaseReview();

        List<ProductTemplateModel> productionTemplateModels = new ArrayList<>();
        if (productionTemplate != null) {
            productionTemplateModels = productionTemplate.getProductTemplateModels();
        }

        ProductTemplate checkTemplate = templateMapper.checkTemplate(resMap);
        List<ProductTemplateModel> checkTemplateModels = new ArrayList<>();
        if (checkTemplate != null) {
            checkTemplateModels = checkTemplate.getProductTemplateModels();
        }

        try {
            if (mateTemplate != null && mateTemplate.getMateTemplateModel() != null && mateTemplate.getMateProductDevice() != null && mateTemplate.getMateAsepsis() != null && mateTemplate.getMateEthylene() != null) {
                java.util.Date day = new Date();
                MateTemplateModel mateTemplateModel = mateTemplate.getMateTemplateModel();
                MateAsepsis mateAsepsis = mateTemplate.getMateAsepsis();
                MateEthylene mateEthylene = mateTemplate.getMateEthylene();
                List<MateProductDevice> mateProductDevices = mateTemplate.getMateProductDevice();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");

                mateProductionPlan.setDeptId(deptId);
                mateProductionPlan.setCompileName(mateTemplateModel.getPlanCompileName());
                mateProductionPlan.setAuditName(mateTemplateModel.getPlanAuditName());
                mateProductionPlan.setAuditTime(mateTemplateModel.getPlanAuditTime());
                mateProductionPlan.setProductId(mateTemplate.getProductId());
                mateProductionPlan.setCompileDate(mateTemplateModel.getPlanCompileDate());
                mateProductionPlan.setPlanStatus("1");
                mateProductionPlan.setPlanOrderNum(mateTemplateModel.getPlanOrderNum());
                mtProductionMapper.addMatePlan(mateProductionPlan);

                MateProductionPlanModel mateProductionPlanModel = new MateProductionPlanModel();
                mateProductionPlanModel.setDeptId(deptId);
                mateProductionPlanModel.setPlanDate(mateTemplateModel.getPlanDate());
                mateProductionPlanModel.setPlanId(mateProductionPlan.getPlanId());
                mateProductionPlanModel.setProductName(mateTemplate.getProductName());
                mateProductionPlanModel.setProductType(mateTemplate.getProductType());
                mateProductionPlanModel.setPlanNum(Long.valueOf(mateTemplate.getPlanNum()));
                mateProductionPlanModel.setRemark(mateTemplateModel.getPlanRemark());
                mtProductionMapper.addMatePlanModel(mateProductionPlanModel);

                mateRequistion.setDeptId(deptId);
                mateRequistion.setQzDate(mateTemplateModel.getReququistionDate());
                mateRequistion.setRequistionOrderNum(mateTemplateModel.getReququistionOrderNum());
                mateRequistion.setDeptPerson(mateTemplateModel.getReququistionDeptPerson());
                mateRequistion.setRealityPerson(mateTemplateModel.getReququistionRealityPerson());
                mateRequistion.setProductId(mateTemplate.getProductId());
                mateRequistion.setSendPerson(mateTemplateModel.getReququistionSendPerson());
                mateRequistion.setRqStatus("1");
                mateRequistionMapper.addMateRequistion(mateRequistion);
                MateRequistionModel mateRequistionModel = new MateRequistionModel();

                releaseReview.setProductId(mateTemplate.getProductId());
                releaseReview.setDeptId(deptId);
                releaseReview.setProductName(mateTemplate.getProductName());
                releaseReview.setProductType(mateTemplate.getProductType());
                releaseReview.setProductionBatch(mateTemplate.getMateBatch());
                releaseReview.setProductionNum(mateTemplate.getProductionNum());
                releaseReview.setProductionAuditId(mateTemplateModel.getProductionAuditId());
                releaseReview.setProductionAuditName(mateTemplateModel.getProductionAuditName());
                releaseReview.setProductionAuditDate(mateTemplateModel.getProductionAuditDate());
                releaseReview.setQualityDate(mateTemplateModel.getReleaseQualityDate());
                releaseReview.setQualityId(mateTemplateModel.getReleaseQualityId());
                releaseReview.setQualityName(mateTemplateModel.getReleaseQualityName());
                releaseReview.setReleaseUserId(mateTemplateModel.getReleaseId());
                releaseReview.setReleaseName(mateTemplateModel.getReleaseName());
                releaseReview.setReleaseDate(mateTemplateModel.getReleaseDate());
                releaseReview.setReleaseOrderNum(mateTemplateModel.getReleaseOrderNum());
                mtProductionMapper.addReleaseReview(releaseReview);

                for (MateProductDevice mateProductDevice : mateProductDevices) {
                    mateRequistionModel.setDeptId(deptId);
                    mateRequistionModel.setRequistionId(mateRequistion.getRequistionId());
                    mateRequistionModel.setMateproductId(mateProductDevice.getMateproductId());
                    mateRequistionModel.setMateBatch(mateProductDevice.getMateBatch());
                    mateRequistionModel.setMaterialId(mateProductDevice.getMaterialId());
                    mateRequistionModel.setMateProductName(mateProductDevice.getMateProductName());
                    mateRequistionModel.setMateProductType(mateProductDevice.getMateProductType());
                    mateRequistionModel.setAskNum(mateProductDevice.getAllNum() + mateProductDevice.getUnit());
                    mateRequistionModel.setRealityNum(mateProductDevice.getAllNum() + mateProductDevice.getUnit());
                    mateRequistionModel.setUnit(mateProductDevice.getUnit());
                    mateRequistionModel.setEnterNum(mateProductDevice.getEnterNum());
                    mateRequistionMapper.addMateRequistionModel(mateRequistionModel);
                }

                MtAudit mtAudit = new MtAudit();
                mtAudit.setCompanyId(SecurityUtils.getDeptId());
                mtAudit.setOrderId(mateRequistion.getRequistionId());
                mtAudit.setZjName(mateRequistion.getDeptPerson());
                mtAudit.setOrderType("5");
                mtAudit.setOrderNum(mateRequistion.getRequistionOrderNum());
                mtAudit.setAuditStatus("1");
                mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit);

                for (MateProductDevice mateProductDevice : mateProductDevices) {
                    mateThrough.setDeptId(deptId);
                    mateThrough.setMateBatch(mateProductDevice.getMateBatch());
                    mateThrough.setEnterNum(mateProductDevice.getEnterNum());
                    mateThrough.setRealityNum(mateProductDevice.getAllNum() + mateProductDevice.getUnit());
                    mateThrough.setMateproductId(mateProductDevice.getMateproductId());
                    mateThrough.setThroughStatus("1");

                    mateThrough.setProcurement("合格");
                    mateThrough.setCheckInformation("合格");
                    mateThrough.setSampling("合格");
                    mateThrough.setResult("合格");
                    mateThrough.setCheckout("合格");
                    mateThrough.setStoreDepict(mateProductDevice.getThroughStoreDepict());
                    mateThrough.setProductId(mateTemplate.getProductId());
                    mateThrough.setMaterialId(mateProductDevice.getMaterialId());
                    mateThrough.setConclusion("符合规定，同意投入使用");
                    mateThrough.setThroughNum(mateTemplateModel.getThroughOrderNum());
                    mateThrough.setYxTime(mateProductDevice.getThroughYxTime());
                    mateThrough.setAuditName(mateTemplateModel.getAuditName());
                    mateThrough.setMateProductName(mateProductDevice.getMateProductName());
                    String TimeStart1 = mateTemplateModel.getThroughAuditTime().replace("Z", " UTC");
                    // Date callbackTimeStart1 = format.parse(TimeStart1 + "00:00:00");
                    Date callbackTimeStart1 = DateUtils.parseDate(mateTemplateModel.getThroughAuditTime());
                    mateThrough.setAuditTime(callbackTimeStart1);
                    mateThroughMapper.addThrough(mateThrough);

                }

                MtAudit mtAudit1 = new MtAudit();
                mtAudit1.setCompanyId(SecurityUtils.getDeptId());
                mtAudit1.setOrderId(mateThrough.getThroughId());
                mtAudit1.setZjName(mateThrough.getAuditName());
                mtAudit1.setOrderType("6");
                mtAudit1.setOrderNum(mateThrough.getThroughNum());
                mtAudit1.setAuditStatus("1");
                mtAudit1.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit1);

                mateTransfer.setProductId(mateTemplate.getProductId());
                mateTransfer.setProductName(mateTemplate.getProductName());
                mateTransfer.setProductType(mateTemplate.getProductType());
                mateTransfer.setMateBatch(mateTemplate.getMateBatch());
                mateTransfer.setDeptId(deptId);
                mateTransfer.setProductionDate(mateTemplate.getProductionDate());
                mateTransfer.setQualityName(mateTemplateModel.getQualityName());
                mateTransfer.setGroupName(mateTemplateModel.getGroupName());
                mateTransfer.setTransferNum(mateTemplateModel.getTransferNum());
                mateTransfer.setTransferStatus("1");

                mateTransferMapper.addMateTransfer(mateTransfer);
                MateTransferModel mateTransferModel = new MateTransferModel();
                for (MateProductDevice mateProductDevice : mateProductDevices) {
                    mateTransferModel.setTransferId(mateTransfer.getTransferId());
                    mateTransferModel.setDeptId(deptId);
                    mateTransferModel.setProcessOperator(mateTemplateModel.getProcessOperator());
                    mateTransferModel.setOperatorName(mateTemplate.getOperatorName());
                    mateTransferModel.setMateProductName(mateProductDevice.getMateProductName());
                    mateTransferModel.setEnterNum(mateProductDevice.getEnterNum());

                    mateTransferModel.setMateproductId(mateProductDevice.getMateproductId());
                    mateTransferMapper.addMateTransferModel(mateTransferModel);
                }

                MateTransferInformation mateTransferInformation = new MateTransferInformation();
                for (ProductTemplateModel productTemplateModel : productionTemplateModels) {
                    mateTransferInformation.setProcessName(productTemplateModel.getProcessName());
                    mateTransferInformation.setTitle(productTemplateModel.getTitle());
                    mateTransferInformation.setTransferId(mateTransfer.getTransferId());
                    mateTransferInformation.setWorkShop(productTemplateModel.getWorkShop());
                    mateTransferInformation.setProcessOperator(productTemplateModel.getProcessOperation());
                    mateTransferInformation.setOperatorName(mateTemplate.getOperatorName());
                    mateTransferInformation.setDeptId(deptId);
                    mateTransferInformation.setProcessNum(productTemplateModel.getProcessNum());
                    mateTransferInformation.setQualifiedNum(mateTemplate.getQualifiedNum());
                    mateTransferMapper.addMateTransferInformation(mateTransferInformation);
                }

                MtAudit mtAudit2 = new MtAudit();
                mtAudit2.setCompanyId(SecurityUtils.getDeptId());
                mtAudit2.setOrderId(mateTransfer.getTransferId());
                mtAudit2.setZjName(mateTransfer.getQualityName());
                mtAudit2.setOrderType("11");
                mtAudit2.setOrderNum(mateTransfer.getTransferNum());
                mtAudit2.setAuditStatus("1");
                mtAudit2.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAudit2.setAuditTime(day);
                mtAuditMapper.insertMtAudit(mtAudit2);

                mateProductionRecord.setIsCreateCheck("1");
                mateProductionRecord.setRecordStatus("1");
                mateProductionRecord.setTransferId(mateTransfer.getTransferId());
                mateProductionRecord.setProductName(mateTemplate.getProductName());
                mateProductionRecord.setProductionDate(mateTemplate.getProductionDate());
                mateProductionRecord.setProductType(mateTemplate.getProductType());
                mateProductionRecord.setMateBatch(mateTemplate.getMateBatch());
                mateProductionRecord.setProductionNum(mateTemplate.getProductionNum());
                mateProductionRecord.setProductionPeriod(mateTemplate.getProductionPeriod());
                mateProductionRecord.setDeptId(SecurityUtils.getDeptId());
                mateProductionRecord.setProductId(mateTemplate.getProductId());
                mateProductionRecord.setRecordOrderNum(mateTemplateModel.getRecordOrderNum());
                mtRecordMapper.addMateRecord(mateProductionRecord);

                MateRecordInformation mateRecordInformation = new MateRecordInformation();
                MateProductionRecordModel mateProductionRecordModel = new MateProductionRecordModel();

                for (MateProductDevice mateProductDevice : mateProductDevices) {
                    MateWarehousing mateWarehousing = new MateWarehousing();
                    String mateproductType = mateProductDevice.getMateProductType();
                    String enterNum = mateProductDevice.getEnterNum();
                    int num = Math.toIntExact(mateProductDevice.getAllNum());

                    mateRecordInformation.setMtRecordId(mateProductionRecord.getMtRecordId());
                    mateRecordInformation.setDeptId(deptId);
                    mateRecordInformation.setMateProductName(mateProductDevice.getMateProductName());
                    mateRecordInformation.setEnterNum(mateProductDevice.getEnterNum());
                    mateRecordInformation.setNeedNum(mateProductDevice.getAllNum());
                    mateRecordInformation.setWarehousingId(mateProductDevice.getWarehousingId());
                    mateRecordInformation.setMateProductType(mateProductDevice.getMateProductType());
                    mateRecordInformation.setMateproductId(mateProductDevice.getMateproductId());
                    mateRecordInformation.setProductId(mateTemplate.getProductId());
                    mtRecordMapper.addMateRecordInformation(mateRecordInformation);

                    mateWarehousing.setDeptId(deptId);
                    mateWarehousing.setEnterNum(enterNum);
                    mateWarehousing.setMateProductType(mateproductType);
                    // mateWarehousing.setWarehousingId(mateProductDevice.getWarehousingId());
                    mateWarehousing.setMateproductId(mateProductDevice.getMateproductId());
                    MateWarehousing mateWarehousing1 = mateWarehousingMapper.selectMateWarehousing(mateWarehousing);

                    // 库存出库日期
                    Date exportDate = new SimpleDateFormat("yyyy-MM-dd").parse(mateTemplateModel.getReququistionDate());
                    if (mateWarehousing1 == null) {
                        mateWarehousingMapper.insertMateWarehousing(mateWarehousing);
                    } else {
                        int warehousingNum = mateWarehousing1.getWarehousingNum();
                        int warehousingNum1 = warehousingNum - num;
                        if (warehousingNum1 < 0) {
                            new AjaxResult(AjaxResult.Type.ERROR, "库存不足");
                            throw new RuntimeException("库存不足");
                        }
                        mateWarehousing.setWarehousingNum(warehousingNum1);
                        mateWarehousing.setWarehousingId(mateWarehousing1.getWarehousingId());
                        mateWarehousing.setUpdateTime(exportDate);
                        mateWarehousingMapper.updateMateWarehousing(mateWarehousing);
                    }
                    // 保存出库操作记录
                    WarehousingRecords wr = new WarehousingRecords();
                    wr.setWarehousingId(mateWarehousing1.getWarehousingId()); // 库存ID
                    wr.setSupplierId(mateProductDevice.getMaterialId()); // 供应商ID

                    int allNum = Optional.ofNullable(mateProductDevice.getAllNum())
                            .map(Long::intValue)
                            .orElse(0);
                    wr.setInventoryNum(-allNum); // 操作数量
                    wr.setInventoryStatus("1"); // 操作类型 ： 出库
                    wr.setCreateTime(exportDate); // 操作时间
                    wr.setCreateBy(SecurityUtils.getLoginUser().getNickName()); // 操作人
                    wr.setAllnum(allNum); // 操作数量
                    wr.setOrderNum("YCG-" + new SimpleDateFormat("yyyyMMddHHmm").format(new Date())); // 采购订单号
                    warehousingRecordsMapper.insertWarehousingRecords(wr);


                }

                for (ProductTemplateModel productTemplateModel : productionTemplateModels) {
                    mateProductionRecordModel.setDeptId(deptId);
                    mateProductionRecordModel.setMtRecordId(mateProductionRecord.getMtRecordId());
                    mateProductionRecordModel.setProductNum(mateTemplate.getProductionNum());
                    mateProductionRecordModel.setProductDate(mateTemplate.getProductionDate());
                    if (productTemplateModel.getMtdeviceId() != 0) {
                        Map<String, Object> deviceMap = new HashMap<>();
                        deviceMap.put("deptId", deptId);
                        deviceMap.put("mtdeviceId", productTemplateModel.getMtdeviceId());
                        MateUserDevice mateUserDevice = new MateUserDevice();
                        MateDevice mateDevice = mateDeviceMapper.selectMateDeviceListById(deviceMap);
                        mateUserDevice.setDeptId(deptId);
                        mateUserDevice.setMtdeviceId(productTemplateModel.getMtdeviceId());
                        mateUserDevice.setDeviceStatus("正常");
                        mateUserDevice.setDeviceName(mateDevice.getDeviceName());
                        mateUserDevice.setRunStatus("正常");
                        mateUserDevice.setDeviceNum(mateDevice.getDeviceNum());
                        mateUserDevice.setIsCalibration(mateDevice.getIsCalibration());
                        mateUserDevice.setParameter(mateDevice.getTeParameter());
                        mateUserDevice.setUserDate(mateTemplate.getProductionDate());
                        mateUserDevice.setProcessName(productTemplateModel.getProcessName());
                        mateUserDevice.setIsProduction(mateDevice.getIsProduction());
                        mateUserDevice.setOperatorName(mateTemplate.getOperatorName());
                        mateUserDevice.setProcessOperation(productTemplateModel.getProcessOperation());
                        mateDeviceMapper.insertUserDevice(mateUserDevice);
                    }

                    if (productTemplateModel.getCheckDeviceId() != 0) {
                        Map<String, Object> deviceMap = new HashMap<>();
                        deviceMap.put("deptId", deptId);
                        deviceMap.put("mtdeviceId", productTemplateModel.getCheckDeviceId());
                        MateUserDevice mateUserDevice = new MateUserDevice();
                        MateDevice mateDevice = mateDeviceMapper.selectMateDeviceListById(deviceMap);
                        mateUserDevice.setDeptId(deptId);
                        mateUserDevice.setMtdeviceId(productTemplateModel.getCheckDeviceId());
                        mateUserDevice.setDeviceStatus("正常");
                        mateUserDevice.setDeviceName(mateDevice.getDeviceName());
                        mateUserDevice.setRunStatus("正常");
                        mateUserDevice.setDeviceNum(mateDevice.getDeviceNum());
                        mateUserDevice.setIsCalibration(mateDevice.getIsCalibration());
                        mateUserDevice.setParameter(mateDevice.getTeParameter());
                        mateUserDevice.setUserDate(mateTemplate.getProductionDate());
                        mateUserDevice.setProcessName(productTemplateModel.getProcessName());
                        mateUserDevice.setOperatorName(String.valueOf(mateTemplate.getOperatorName()));
                        mateUserDevice.setIsProduction(mateDevice.getIsProduction());
                        mateUserDevice.setProcessOperation(productTemplateModel.getProcessOperation());
                        mateDeviceMapper.insertUserDevice(mateUserDevice);

                    }

                    mateProductionRecordModel.setDeviceNum(productTemplateModel.getDeviceName());
                    mateProductionRecordModel.setProcessName(productTemplateModel.getProcessName());
                    mateProductionRecordModel.setTeParameter(productTemplateModel.getTeParameter());
                    mateProductionRecordModel.setOperatorName(mateTemplate.getOperatorName());
                    mateProductionRecordModel.setTitle(productTemplateModel.getTitle());
                    mateProductionRecordModel.setProcessNum(productTemplateModel.getProcessNum());
                    mtRecordMapper.addMateRecordModel(mateProductionRecordModel);
                }

                for (ProductTemplateModel checkTemplateModel : checkTemplateModels) {
                    if (checkTemplateModel.getCheckDeviceId() != 0) {
                        Map<String, Object> checkMap = new HashMap<>();
                        checkMap.put("deptId", deptId);
                        checkMap.put("mtdeviceId", checkTemplateModel.getCheckDeviceId());
                        MateUserDevice mateUserDevice = new MateUserDevice();
                        MateDevice mateDevice = mateDeviceMapper.selectMateDeviceListById(checkMap);
                        mateUserDevice.setDeptId(deptId);
                        mateUserDevice.setMtdeviceId(checkTemplateModel.getCheckDeviceId());
                        mateUserDevice.setDeviceStatus("正常");
                        mateUserDevice.setDeviceName(mateDevice.getDeviceName());
                        mateUserDevice.setRunStatus("正常");
                        mateUserDevice.setDeviceNum(mateDevice.getDeviceNum());
                        mateUserDevice.setIsCalibration(mateDevice.getIsCalibration());
                        mateUserDevice.setParameter(mateDevice.getTeParameter());
                        mateUserDevice.setUserDate(mateTemplate.getProductionDate());
                        mateUserDevice.setProcessName(checkTemplateModel.getDetectionProject());
                        mateUserDevice.setOperatorName(mateTemplate.getOperatorName());
                        mateUserDevice.setIsProduction(mateDevice.getIsProduction());
                        mateDeviceMapper.insertUserDevice(mateUserDevice);
                    }
                }

                MtAudit mtAudit3 = new MtAudit();
                mtAudit3.setCompanyId(SecurityUtils.getDeptId());
                mtAudit3.setOrderId(mateProductionRecord.getMtRecordId());
                mtAudit3.setZjName(mateProductionRecordModel.getOperatorName());
                mtAudit3.setOrderType("8");
                mtAudit3.setOrderNum(mateProductionRecord.getRecordOrderNum());
                mtAudit3.setAuditStatus("1");
                mtAudit3.setAuditTime(day);
                mtAudit3.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit3);

                mtCourseCheck.setIsCreateCleaning("1");
                mtCourseCheck.setCourseStatus("1");
                mtCourseCheck.setMtRecordId(mateProductionRecord.getMtRecordId());

                mtCourseCheck.setCourseOrderNum(mateTemplateModel.getCourseOrderNum());
                mtCourseCheck.setProductType(mateTemplate.getProductType());
                mtCourseCheck.setDeptId(deptId);
                mtCourseCheck.setProductName(mateTemplate.getProductName());
                mtCourseCheck.setMateBatch(mateTemplate.getMateBatch());
                mtCourseCheck.setProductionNum(mateTemplate.getProductionNum());
                mtCourseCheck.setProductionDate(mateTemplate.getProductionDate());
                mtCourseCheck.setZjName(mateTemplateModel.getZjName());
                mtCourseCheck.setAuditName(mateTemplateModel.getAuditName());
                mtCourseCheck.setRatifyName(mateTemplate.getOperatorName());
                mtCourseCheck.setProductId(mateTemplate.getProductId());
                mateCourseMapper.addMateCourse(mtCourseCheck);

                MtCourseProduct mtCourseProduct = new MtCourseProduct();
                MtCourseProject mtCourseProject = new MtCourseProject();

                for (MateProductDevice mateProductDevice : mateProductDevices) {
                    mtCourseProduct.setDeptId(deptId);
                    mtCourseProduct.setMtCourseId(mtCourseCheck.getMtCourseId());
                    mtCourseProduct.setMateProductName(mateProductDevice.getMateProductName());
                    mtCourseProduct.setMateproductId(mateProductDevice.getMateproductId());
                    mtCourseProduct.setNeedNum(mateProductDevice.getAllNum());
                    mtCourseProduct.setEnterNum(mateProductDevice.getEnterNum());
                    mateCourseMapper.addMateCourseProduct(mtCourseProduct);
                }

                for (ProductTemplateModel productTemplateModel : productionTemplateModels) {
                    mtCourseProject.setDeptId(deptId);
                    mtCourseProject.setMtCourseId(mtCourseCheck.getMtCourseId());
                    mtCourseProject.setCheckInspection(productTemplateModel.getCheckInspection());
                    mtCourseProject.setProcessName(productTemplateModel.getProcessName());
                    mtCourseProject.setTitle(productTemplateModel.getTitle());
                    mtCourseProject.setProcessNum(productTemplateModel.getProcessNum());
                    mtCourseProject.setCheckProject(productTemplateModel.getDetectionProject());
                    if (productTemplateModel.getIsNum() == 0) {
                        Double minValue = productTemplateModel.getMinNum();
                        Double maxValue = productTemplateModel.getMaxNum();
                        double randomValue = minValue + (maxValue - minValue) * new Random().nextDouble();
                        // 保留两位小数
                        DecimalFormat df = new DecimalFormat("#.##");
                        String formattedResult = df.format(randomValue);
                        mtCourseProject.setCheckResult(productTemplateModel.getCheckResult() + formattedResult + productTemplateModel.getUnit());

                    } else {
                        mtCourseProject.setCheckResult("合格");
                    }

                    mtCourseProject.setResultDecide("合格");
                    mateCourseMapper.addMateCourseProject(mtCourseProject);
                }

                MtCourseSample mtCourseSample = new MtCourseSample();
                for (int i = 0; i < Integer.valueOf(mateTemplate.getCheckNum()); i++) {
                    int nums = i + 1;
                    mtCourseSample.setDeptId(deptId);
                    mtCourseSample.setMtCourseId(mtCourseCheck.getMtCourseId());
                    mtCourseSample.setSampleName("样本" + nums);
                    mateCourseMapper.addMateCourseSample(mtCourseSample);
                }

                MtAudit mtAudit4 = new MtAudit();
                mtAudit4.setCompanyId(SecurityUtils.getDeptId());
                mtAudit4.setOrderId(mtCourseCheck.getMtCourseId());
                mtAudit4.setZjName(mtCourseCheck.getZjName());
                mtAudit4.setOrderType("10");
                mtAudit4.setOrderNum(mtCourseCheck.getCourseOrderNum());
                mtAudit4.setAuditTime(day);
                mtAudit4.setAuditStatus("1");
                mtAudit4.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit4);

                mateCleanRecord.setIsCreatDaily("1");
                mateCleanRecord.setCleanStatus("1");
                mateCleanRecord.setDeptId(deptId);
                mateCleanRecord.setProductName(mateTemplate.getProductName());
                mateCleanRecord.setCleanOrderNum(mateTemplateModel.getCleanOrderNum());
                mateCleanRecord.setMtCourseId(mtCourseCheck.getMtCourseId());
                mateCleanRecord.setOperatorRequest("每批产品生产完毕时，进行清场\n" +
                        "1、将本批中间产品、废弃物、剩余物料清理现场，无遗留物。各状态标识符合清场后\n" +
                        "   状态。\n" +
                        "2、按清场SOP清洁生产设备，做到内外设备无油污、干净、无物料遗留物。\n" +
                        "3、按清场SOP清洁(或清扫)工具、容器，做到清洁、无异物、无物料遗留物。\n" +
                        "4、按清场SOP清洁地面、墙壁、门窗、天棚、地漏、工作台面，开关箱外壳，做到\n" +
                        "   无积尘、无物料碎屑。\n" +
                        "5、按清场SOP清洗卫生工具，做到干净、无遗留物，并按规定进行放置。");

                mateCleanRecord.setProductType(mateTemplate.getProductType());
                mateCleanRecord.setProductId(mateTemplate.getProductId());
                mateCleanRecord.setMateBatch(mateTemplate.getMateBatch());
                mateCleanRecord.setProductionDate(mateTemplate.getProductionDate());
                mateCleanRecord.setNeedNum(Long.valueOf(mateTemplate.getProductionNum()));
                mateCleanRecord.setWorkShop("洁净车间");
                mateCleanRecord.setCleanName(mateTemplateModel.getCleanName());
                mateCleanRecord.setGroupName(mateTemplateModel.getCelanGroupName());
                mateCleanRecord.setGroupDate(mateTemplateModel.getGroupDate());
                mateCleanRecord.setCheckName(mateTemplateModel.getCheckName());
                mateCleanRecord.setCheckDate(mateTemplateModel.getCheckDate());
                mateCleanRecordMapper.addMateCleanRecord(mateCleanRecord);

                MateCleanRecordModel mateCleanRecordModel = new MateCleanRecordModel();
                mateCleanRecordModel.setCleanId(mateCleanRecord.getCleanId());
                mateCleanRecordModel.setCleanProject("物料");
                mateCleanRecordModel.setCleanRequest("结料、剩余物料退料");
                mateCleanRecordModel.setCleanResult("合格");
                mateCleanRecordModel.setDeptId(deptId);
                mateCleanRecordMapper.addMateCleanRecordModel(mateCleanRecordModel);

                MtAudit mtAudit5 = new MtAudit();
                mtAudit5.setCompanyId(SecurityUtils.getDeptId());
                mtAudit5.setOrderId(mateCleanRecord.getCleanId());
                mtAudit5.setZjName(mateCleanRecord.getCheckName());
                mtAudit5.setOrderType("12");
                mtAudit5.setOrderNum(mateCleanRecord.getCleanOrderNum());
                mtAudit5.setAuditStatus("1");
                mtAudit5.setAuditTime(day);
                mtAudit5.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit5);

                mateDaily.setIsCreateCheck("1");
                mateDaily.setDailyStatus("1");
                mateDaily.setProductId(productId);
                mateDaily.setDeptId(deptId);
                mateDaily.setDailyOrderNum(mateTemplateModel.getDailyOrderNum());
                mateDaily.setAuditName(mateTemplateModel.getDailyAuditName());
                mateDaily.setAssuranceName(mateTemplateModel.getDailyAssuranceName());
                mateDaily.setCleanId(mateCleanRecord.getCleanId());
                mateDailyMapper.addMateDaily(mateDaily);

                MateDailyModel mateDailyModel = new MateDailyModel();
                mateDailyModel.setProductType(mateTemplate.getProductType());
                mateDailyModel.setDailyId(mateDaily.getDailyId());
                mateDailyModel.setDeptId(deptId);
                mateDailyModel.setProductId(productId);
                mateDailyModel.setProductName(mateTransfer.getProductName());
                mateDailyModel.setMateBatch(mateTemplate.getMateBatch());
                mateDailyModel.setProductionDate(mateTemplate.getProductionDate());
                mateDailyModel.setRelustNum(mateTemplate.getProductionNum());
                mateDailyModel.setProductType(mateTemplate.getProductType());
                mateDailyModel.setProductionNum(mateTemplate.getProductionNum());
                mateDailyModel.setExamineNum(mateTemplate.getExamineNum());
                mateDailyModel.setQualifiedNum(mateTemplate.getQualifiedNum());
                mateDailyModel.setPassRate(mateTemplate.getPassRate());
                mateDailyModel.setConfirmName(mateTemplate.getConfirmName());
                mateDailyModel.setProductionDate(mateTemplate.getProductionDate());
                mateDailyMapper.addMateDailyModel(mateDailyModel);

                MtAudit mtAudit6 = new MtAudit();
                mtAudit6.setCompanyId(SecurityUtils.getDeptId());
                mtAudit6.setOrderId(mateDaily.getDailyId());
                mtAudit6.setZjName(mateDaily.getAuditName());
                mtAudit6.setOrderType("9");
                mtAudit6.setAuditTime(day);
                mtAudit6.setOrderNum(mateDaily.getDailyOrderNum());
                mtAudit6.setAuditStatus("1");
                mtAudit6.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit6);

                checkInformation.setIsCreatReport("1");
                checkInformation.setCheckOrderNum(mateTemplateModel.getCheckOrderNum());
                checkInformation.setInformationStatus("1");
                checkInformation.setProductName(mateTemplate.getProductName());
                checkInformation.setProductId(mateTemplate.getProductId());
                checkInformation.setDailyId(mateDaily.getDailyId());
                checkInformation.setCheckDate(mateTemplateModel.getProductCheckDate());
                checkInformation.setProductType(mateTemplate.getProductType());
                checkInformation.setMateBatch(mateTemplate.getMateBatch());
                checkInformation.setZjName(mateTemplateModel.getReportZjName());
                checkInformation.setReviewer(mateTemplateModel.getReportAuditName());
                checkInformation.setProductionDate(mateTemplate.getProductionDate());
                checkInformation.setRecordNum(mateTemplate.getProductionNum());
                checkInformation.setCheckNum(mateTemplate.getCheckNum());
                checkInformation.setDeptId(deptId);
                checkInformation.setProductionCompany(mateTemplate.getProductionCompany());
                checkInformationMapper.addInformation(checkInformation);
                CheckInformationModel checkInformationModel = new CheckInformationModel();

                for (ProductTemplateModel productTemplateModel : checkTemplateModels) {
                    checkInformationModel.setCheckId(checkInformation.getCheckId());
                    checkInformationModel.setDeptId(deptId);
                    if (productTemplateModel.getIsNum() == 0) {
                        Double minValue = productTemplateModel.getMinNum();
                        Double maxValue = productTemplateModel.getMaxNum();
                        double randomValue = minValue + (maxValue - minValue) * new Random().nextDouble();
                        // 保留两位小数
                        DecimalFormat df = new DecimalFormat("#.##");
                        String formattedResult = df.format(randomValue);
                        checkInformationModel.setCheckResult(productTemplateModel.getCheckResult() + formattedResult + productTemplateModel.getUnit());
                    } else {
                        checkInformationModel.setCheckResult("合格");
                    }
                    checkInformationModel.setCheckProject(productTemplateModel.getDetectionProject());
                    checkInformationModel.setCheckRequest(productTemplateModel.getCheckInspection());
                    checkInformationModel.setCheckConclusion("合格");
                    checkInformationModel.setProductId(productId);
                    checkInformationMapper.addInformationModel(checkInformationModel);
                }

                CheckSample checkSample = new CheckSample();
                for (int i = 0; i < Integer.valueOf(mateTemplate.getCheckNum()); i++) {
                    int nums = i + 1;
                    checkSample.setDeptId(deptId);
                    checkSample.setCheckId(checkInformation.getCheckId());
                    checkSample.setSampleName("样本" + nums);
                    checkInformationMapper.addCheckSample(checkSample);
                }

                MtAudit mtAudit7 = new MtAudit();
                mtAudit7.setCompanyId(SecurityUtils.getDeptId());
                mtAudit7.setOrderId(checkInformation.getCheckId());
                mtAudit7.setOrderType("13");
                mtAudit7.setAuditTime(day);
                mtAudit7.setOrderNum(checkInformation.getCheckOrderNum());
                mtAudit7.setAuditStatus("1");
                mtAudit7.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit7);

                productReport.setDeptId(deptId);
                productReport.setIsCreatRk("1");
                productReport.setProductName(mateTemplate.getProductName());
                productReport.setReportStatus("1");
                productReport.setCheckId(checkInformation.getCheckId());
                productReport.setProductType(mateTemplate.getProductType());
                productReport.setMateBatch(mateTemplate.getMateBatch());
                productReport.setProductId(productId);
                productReport.setCheckDate(mateTemplateModel.getProductCheckDate());
                productReport.setProductionDate(mateTemplate.getProductionDate());
                productReport.setRecordNum(mateTemplate.getProductionNum());
                productReport.setCheckNum(mateTemplate.getCheckNum());
                productReport.setReportOrderNum(mateTemplateModel.getReportOrderNum());
                productReport.setRemark(mateTemplateModel.getRemark());
                productReport.setZjName(mateTemplateModel.getReportZjName());
                productReport.setAuditName(mateTemplateModel.getReportAuditName());
                productReport.setQualityName(mateTemplateModel.getReportQualityName());
                productReport.setQualityDate(mateTemplateModel.getQualityDate());
                productReport.setStampDate(mateTemplateModel.getStampDate());
                productReport.setCheckCategory(mateTemplateModel.getCheckCategory());
                productReport.setCheckAccording(mateTemplateModel.getCheckAccording());
                productReport.setProductGrade(mateTemplateModel.getProductGrade());
                productReport.setCheckEnvironment(mateTemplate.getCheckEnvironment());
                productReport.setCheckResult(mateTemplateModel.getReportInspection());
                productReportMapper.addReport(productReport);

                ProductReportModel productReportModel = new ProductReportModel();

                for (ProductTemplateModel productTemplateModel : checkTemplateModels) {
                    productReportModel.setProcessNum(productTemplateModel.getProcessNum());
                    productReportModel.setReportId(productReport.getReportId());
                    productReportModel.setDeptId(deptId);
                    if (productTemplateModel.getIsNum() == 0) {
                        Double minValue = productTemplateModel.getMinNum();
                        Double maxValue = productTemplateModel.getMaxNum();
                        double randomValue = minValue + (maxValue - minValue) * new Random().nextDouble();
                        // 保留两位小数
                        DecimalFormat df = new DecimalFormat("#.##");
                        String formattedResult = df.format(randomValue);
                        productReportModel.setCheckResult(productTemplateModel.getCheckResult() + formattedResult + productTemplateModel.getUnit());
                    } else {
                        productReportModel.setCheckResult("合格");
                    }
                    productReportModel.setDetectionProject(productTemplateModel.getDetectionProject());
                    productReportModel.setCheckInspection(productTemplateModel.getCheckInspection());
                    productReportModel.setCheckConclusion("合格");

                    productReportMapper.insertReportModel(productReportModel);
                }

                MtAudit mtAudit8 = new MtAudit();
                mtAudit8.setCompanyId(SecurityUtils.getDeptId());
                mtAudit8.setOrderId(productReport.getReportId());
                mtAudit8.setOrderType("14");
                mtAudit8.setAuditTime(day);
                mtAudit8.setOrderNum(productReport.getReportOrderNum());
                mtAudit8.setAuditStatus("1");
                mtAudit8.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit8);

                Map<String, Object> map = new HashMap<>();
                map.put("deptId", deptId);
                map.put("productId", productId);

                String TimeStart = mateTemplate.getProductionDate().replace("Z", " UTC");
                // Date callbackTimeStart = format.parse(TimeStart);
                Date callbackTimeStart = DateUtils.parseDate(TimeStart);
                finishedProduct.setReportId(productReport.getReportId());
                finishedProduct.setFinishedOrderNum(mateTemplateModel.getFinishedOrderNum());
                finishedProduct.setFinishedProductStatus("1");
                finishedProduct.setDeptId(deptId);
                finishedProductMapper.addFinishedProduct(finishedProduct);
                Product product = productMapper.selectProductProofByMap(map);

                FinishedProductModel finishedProductModel = new FinishedProductModel();
                finishedProductModel.setFinishedProductId(finishedProduct.getFinishedProductId());
                finishedProductModel.setProductId(productId);
                finishedProductModel.setDeptId(deptId);
                finishedProductModel.setProductType(mateTemplate.getProductType());
                finishedProductModel.setBatch(mateTemplate.getMateBatch());
                finishedProductModel.setMedicalNumber(product.getMedicalNumber());
                finishedProductModel.setMedicalEndTime(product.getMedicalEndTime());
                finishedProductModel.setEffectiveDate(mateTemplate.getEffectiveDate());
                finishedProductModel.setProductionDate(callbackTimeStart);
                finishedProductModel.setWarehousingNum(Integer.parseInt(mateTemplate.getProductionNum()));
                finishedProductModel.setWorkshopName(mateTemplate.getWorkshopName());
                finishedProductModel.setAcceptName(mateTemplate.getAcceptName());
                finishedProductModel.setWarehousingDate(mateTemplate.getWarehousingDate());
                finishedProductModel.setUnit(mateTemplate.getUnit());
                finishedProductModel.setWarehousingName(mateTemplate.getWarehousingName());
                finishedProductModel.setProductName(mateTemplate.getProductName());
                finishedProductMapper.addFinishedProductModel(finishedProductModel);

                Warehousing warehousing = new Warehousing();
                warehousing.setMedicalEndTime(product.getEndTime());
                warehousing.setProductType(mateTemplate.getProductType());
                warehousing.setBatch(mateTemplate.getMateBatch());
                warehousing.setMedicalNumber(product.getMedicalNumber());
                warehousing.setProductionDate(callbackTimeStart);
                warehousing.setEffectiveDate(mateTemplate.getEffectiveDate());
                warehousing.setDeptId(deptId);
                warehousing.setSelectId(mateTemplate.getProductId());
                warehousing.setProductId(mateTemplate.getProductId());
                warehousing.setUpdateTime(mateTemplate.getWarehousingDate());

                Warehousing warehousing1 = warehousingMapper.selectWarehousing(warehousing);

                if (warehousing1 == null) {
                    warehousing.setAmount(Long.valueOf(mateTemplate.getProductionNum()));
                    warehousingMapper.insertWarehousing(warehousing);
                } else {
                    Long warehousingNum = warehousing1.getAmount();
                    Long num1 = warehousingNum + Long.valueOf(mateTemplate.getProductionNum());
                    warehousing.setAmount(num1);
                    warehousing.setWarehousingId(warehousing1.getWarehousingId());

                    warehousingMapper.updateWarehousing(warehousing);
                }

                // 入库操作记录操作
                WarehousingRecords warehousingRecords = new WarehousingRecords();
                warehousingRecords.setWarehousingId(warehousing.getWarehousingId()); // 库存ID
                warehousingRecords.setSupplierId(deptId); // 因为是自己生产的，所以供应商ID是我们自己
                warehousingRecords.setInventoryNum(Integer.parseInt(mateTemplate.getProductionNum())); // 操作数量
                warehousingRecords.setInventoryStatus("0"); // 操作类型：入库
                warehousingRecords.setCreateTime(mateTemplate.getWarehousingDate());
                warehousingRecords.setCreateBy(SecurityUtils.getUsername());
                warehousingRecords.setAllnum(Integer.parseInt(mateTemplate.getProductionNum()));
                warehousingRecords.setOrderNum("SC-" + new SimpleDateFormat("yyyyMMddHHmm").format(mateTemplate.getWarehousingDate())); // 订单号：SC-202510201240
                warehousingRecordsMapper.insertWarehousingRecords(warehousingRecords);


                MtAudit mtAudit9 = new MtAudit();
                mtAudit9.setCompanyId(SecurityUtils.getDeptId());
                mtAudit9.setOrderId(finishedProduct.getFinishedProductId());
                mtAudit9.setZjName(mateTemplate.getWarehousingName());
                mtAudit9.setOrderType("15");
                mtAudit9.setOrderNum(finishedProduct.getFinishedOrderNum());
                mtAudit9.setAuditStatus("1");
                mtAudit9.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit9);

                mateAsepsis.setDeptId(deptId);
                mateAsepsis.setProductName(mateTemplate.getProductName());
                mateAsepsis.setProductType(mateTemplate.getProductType());
                mateAsepsis.setMateBatch(mateTemplate.getMateBatch());

                mateAsepsis.setAsepResult("符合规定");
                mateAsepsis.setColonyResult("符合");
                mateAsepsis.setAsepStatus("1");

                mateAsepsis.setProductId(mateTemplate.getProductId());
                mateAsepsisMapper.insertAsepsis(mateAsepsis);

                for (int i = 0; i < 3; i++) {
                    MateAsepsisCultivate mateAsepsisCultivate = new MateAsepsisCultivate();
                    mateAsepsisCultivate.setDeptId(deptId);
                    mateAsepsisCultivate.setAsepsisId(mateAsepsis.getAsepsisId());
                    Random random = new Random();
                    int randomNumber = random.nextInt(2); // 生成0或 1

                    Random random1 = new Random();
                    int randomNumber1 = random1.nextInt(2); // 生成 0 或 1
                    int oneColony = randomNumber;
                    int twoColony = randomNumber1;
                    int averageColony = (oneColony + twoColony) / 2;

                    mateAsepsisCultivate.setOneColony(oneColony);
                    mateAsepsisCultivate.setTwoColony(twoColony);
                    mateAsepsisCultivate.setAverageColony(averageColony);
                    mateAsepsisMapper.insertAsepsisCultivate(mateAsepsisCultivate);
                }
                mateEthylene.setDeptId(deptId);
                mateEthylene.setAsepsisBatch(mateAsepsis.getAsepsisBatch());
                mateEthylene.setProductName(mateTemplate.getProductName());
                mateEthylene.setProductType(mateTemplate.getProductType());
                mateEthylene.setEthyStatus("1");
                mateEthylene.setMateBatch(mateTemplate.getMateBatch());
                mateEthylene.setProductId(mateTemplate.getProductId());
                mateEthyleneMapper.insertMateEthylene(mateEthylene);
            }

        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
            // TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            // mateTransferMapper.removeMateTransferTemplate(mateTransfer.getTransferId());
            // mateTransferMapper.removeMateTransferModelTemplate(mateTransfer.getTransferId());

            // mtRecordMapper.removeMtRecordTemplate(mateProductionRecord.getMtRecordId());
            // mtRecordMapper.removeMtRecordModelTemplate(mateProductionRecord.getMtRecordId());
            // mtRecordMapper.removeMtRecordModelTemplate(mateProductionRecord.getMtRecordId());

            // mateCourseMapper.removeMateCourseTemplate(mtCourseCheck.getMtCourseId());
            // mateCourseMapper.removeMateCourseProductTemplate(mtCourseCheck.getMtCourseId());
            // mateCourseMapper.removeMateCourseProjectTemplate(mtCourseCheck.getMtCourseId());

            // mateDailyMapper.removeMateDailyTemplate(mateDaily.getDailyId());
            // mateDailyMapper.removeMateDailyModelTemplate(mateDaily.getDailyId());

            // checkInformationMapper.removeCheckInformationTemplate(checkInformation.getCheckId());
            // checkInformationMapper.removeCheckInformationModelTemplate(checkInformation.getCheckId());

            // productReportMapper.removeReportTemplate(productReport.getReportId());
//
            // finishedProductMapper.removeFinishedProductTemplate(finishedProduct.getFinishedProductId());
            // finishedProductMapper.removeFinishedProductModelTemplate(finishedProduct.getFinishedProductId());

        }


    }

    @Override
    public List<ProductRelation> selectTemplate(Map<String, Object> resMap) {
        List<ProductRelation> productRelations = productMapper.selectMateProductId(resMap);
        return productRelations;
    }

    @Override
    public Map<String, Object> selectMateTemplate(Long productId) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("productId", productId);
        resMap.put("deptId", SecurityUtils.getDeptId());

        List<MateTransferModel> mateTransferModels = mateTransferMapper.selectMateTransferModelTemplate(resMap);

        List<MateProductionRecordModel> mateProductionRecordModels = mtRecordMapper.selectMtRecordModelTemplate(resMap);

        List<MtCourseProject> mtCourseProjects = mateCourseMapper.selectMtCourseProjectTemplate(resMap);

        List<MateCleanRecordModel> mateCleanRecordModels = mateCleanRecordMapper.selectCleanRecordModelTemplate(resMap);

        List<CheckInformationModel> checkInformationModels = checkInformationMapper.selectInformationModelTemplate(resMap);

        ProductReport productReports = productReportMapper.selectReportTemplate(resMap);

        Map<String, Object> result = new HashMap<>();

        result.put("mateTransferModels", mateTransferModels);
        result.put("mateProductionRecordModels", mateProductionRecordModels);
        result.put("mtCourseProjects", mtCourseProjects);
        result.put("mateCleanRecordModels", mateCleanRecordModels);
        result.put("checkInformationModels", checkInformationModels);
        result.put("productReports", productReports);

        return result;
    }
    private String codeAutoIncrement(String code) {
        String newStr = "";
        Matcher m = Pattern.compile("(\\d+)(?!.*\\d)").matcher(code);
        if (m.find()) newStr = code.replaceFirst("(\\d+)(?!.*\\d)",String.format("%03d", Integer.parseInt(m.group()) + 1));
        return newStr;
    }
    @Override
    public Map<String, Object> selectLastRecord(Long productId) {
        Map<String, Object> resMapa = new HashMap<>();
        MateTemplate mateTemplate = new MateTemplate();
        MateTemplateModel mateTemplateModel = new MateTemplateModel();
        mateTemplate.setMateTemplateModel(mateTemplateModel);

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("deptId", SecurityUtils.getDeptId());

        Map<String,Object> productMap=new HashMap<>();
        productMap.put("productId",productId);
        productMap.put("deptId",SecurityUtils.getDeptId());
        // resMap.put("productId", productId);

        MateProductionPlan mateProductionPlan = mtProductionMapper.selectLastPlan(resMap);

        MateTransfer mateTransfer = mateTransferMapper.selectMateTransferLast(resMap);

        MateRequistion mateRequistion = mateRequistionMapper.selectMateRequistionLast(resMap);

        MateThrough mateThrough = mateThroughMapper.selectMateThroughLast(resMap);

        MateProductionRecord mateProductionRecord = mtRecordMapper.selectMateProductionRecordLast(resMap);

        List<MateProductionRecordModel> mateProductionRecordModels = new ArrayList<>();
        if (mateProductionRecord != null) {
            mateProductionRecordModels = mtRecordMapper.selectRecordModelByRecordId(mateProductionRecord.getMtRecordId());
        }

        MateCleanRecord mateCleanRecord = mateCleanRecordMapper.selectCleanRecordLast(resMap);

        MtCourseCheck mtCourseCheck = mateCourseMapper.selectMateCourseLast(resMap);

        MateDaily mateDaily = mateDailyMapper.selectMateDailyLast(resMap);

        MateDailyModel mateDailyModel = mateDailyMapper.selectMateDailyModelLast(resMap);

        CheckInformation checkInformation = checkInformationMapper.selectInformationLast(resMap);

        ProductReport productReport = productReportMapper.selectReportLast(resMap);

        FinishedProductModel finishedProductModel = finishedProductMapper.selectFinishedProductModelLast(resMap);

        MateEthylene mateEthylene = mateEthyleneMapper.selectLast(resMap);

        MateAsepsis mateAsepsis = mateAsepsisMapper.selectLast(resMap);

        ReleaseReview releaseReview = new ReleaseReview();
        releaseReview.setReleaseUserId(productId);
        releaseReview.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        List<ReleaseReview> releaseReviews = mtProductionMapper.selectReleaseReviewList(releaseReview);
        if (!releaseReviews.isEmpty()) {
            releaseReview = releaseReviews.get(releaseReviews.size() - 1);
        }

        // mateTemplateModel.setFinishedOrderNum(); // 生产报告编号
        mateTemplateModel.setProductionAuditId(releaseReview.getProductionAuditId());
        mateTemplateModel.setProductionAuditName(releaseReview.getProductionAuditName());
        // mateTemplateModel.setProductionAuditDate(releaseReview.getProductionAuditDate());

        mateTemplateModel.setReleaseQualityId(releaseReview.getQualityId());
        mateTemplateModel.setReleaseQualityName(releaseReview.getQualityName());
        // mateTemplateModel.setReleaseQualityDate(releaseReview.getQualityDate());

        mateTemplateModel.setReleaseId(releaseReview.getReleaseUserId());
        mateTemplateModel.setReleaseName(releaseReview.getReleaseName());
        // mateTemplateModel.setReleaseDate(releaseReview.getReleaseDate());

        mateTemplateModel.setReleaseOrderNum(releaseReview.getReleaseOrderNum());


        if (mateTransfer != null && mateRequistion != null && mateThrough != null && mateProductionRecord != null && mtCourseCheck != null && mateCleanRecord != null
                && mateDaily != null && mateDailyModel != null && checkInformation != null && productReport != null && finishedProductModel != null/* && mateAsepsis != null && mateEthylene != null*/) {
            mateTemplateModel.setPlanAuditName(mateProductionPlan.getAuditName());
            mateTemplateModel.setPlanCompileName(mateProductionPlan.getCompileName());



            if (mateProductionPlan.getPlanOrderNum() != null) {
                mateTemplateModel.setPlanOrderNum(codeAutoIncrement(mateProductionPlan.getPlanOrderNum()));
            }


            mateTemplateModel.setQualityName(mateTransfer.getQualityName());
            mateTemplateModel.setGroupName(mateTransfer.getGroupName());

            if (mateTransfer.getTransferNum() != null) {
                mateTemplateModel.setTransferNum(codeAutoIncrement(mateTransfer.getTransferNum()));
            }


            mateTemplateModel.setReququistionDeptPerson(mateRequistion.getDeptPerson());
            mateTemplateModel.setReququistionRealityPerson(mateRequistion.getRealityPerson());
            mateTemplateModel.setReququistionSendPerson(mateRequistion.getSendPerson());

            if (mateRequistion.getRequistionOrderNum() != null) {
                mateTemplateModel.setReququistionOrderNum(codeAutoIncrement(mateRequistion.getRequistionOrderNum()));
            }

            mateTemplateModel.setThroughAuditName(mateThrough.getAuditName());

            if (mateThrough.getThroughNum() != null) {
                mateTemplateModel.setThroughOrderNum(codeAutoIncrement(mateThrough.getThroughNum()));
            }

            if (mateProductionRecordModels.size() != 0) {
                mateTemplate.setOperatorName(mateProductionRecordModels.get(0).getOperatorName());
                mateTemplateModel.setRecordOperatorName(mateProductionRecordModels.get(0).getOperatorName());
            }
            Product product=productMapper.selectProduction(productMap);
            mateTemplate.setMateBatch(mateProductionRecord.getMateBatch());
            mateTemplate.setProductType(mateProductionRecord.getProductType());
            mateTemplate.setProductionPeriod(mateProductionRecord.getProductionPeriod());
            mateTemplate.setProductionNum(mateTemplate.getProductionNum());
            mateTemplate.setProductName(product.getProductName());

            if (mateProductionRecord.getRecordOrderNum() != null) {
                mateTemplateModel.setRecordOrderNum(codeAutoIncrement(mateProductionRecord.getRecordOrderNum()));
            }

            mateTemplateModel.setZjName(mtCourseCheck.getZjName());
            mateTemplateModel.setAuditName(mtCourseCheck.getAuditName());
            mateTemplateModel.setRatifyName(mtCourseCheck.getRatifyName());

            if (mtCourseCheck.getCourseOrderNum() != null) {
                mateTemplateModel.setCourseOrderNum(codeAutoIncrement(mtCourseCheck.getCourseOrderNum()));
            }

            mateTemplateModel.setCleanName(mateCleanRecord.getCleanName());
            mateTemplateModel.setCelanGroupName(mateCleanRecord.getGroupName());
            mateTemplateModel.setCleanworkShop(mateCleanRecord.getWorkShop());
            mateTemplateModel.setCheckName(mateCleanRecord.getCheckName());

            if (mateCleanRecord.getCleanOrderNum() != null) {
                mateTemplateModel.setCleanOrderNum(codeAutoIncrement(mateCleanRecord.getCleanOrderNum()));

            }

            mateTemplateModel.setDailyAuditName(mateDaily.getAuditName());
            mateTemplateModel.setDailyAssuranceName(mateDaily.getAssuranceName());
            mateTemplate.setExamineNum(mateDailyModel.getExamineNum());
            mateTemplate.setQualifiedNum(mateDailyModel.getQualifiedNum());
            mateTemplate.setPassRate(mateDailyModel.getPassRate());
            mateTemplate.setConfirmName(mateDailyModel.getConfirmName());
            mateTemplate.setProductionNum(mateDailyModel.getProductionNum());
            mateTemplate.setPlanNum(mateDailyModel.getProductionNum());

            mateTemplateModel.setDailyOrderNum(codeAutoIncrement(mateDaily.getDailyOrderNum()));


            mateTemplate.setProductionCompany(checkInformation.getProductionCompany());
            if (checkInformation.getCheckOrderNum() != null) {
                mateTemplateModel.setCheckOrderNum(codeAutoIncrement(checkInformation.getCheckOrderNum()));
            }

            mateTemplate.setCheckEnvironment(productReport.getCheckEnvironment());
            mateTemplateModel.setReportZjName(productReport.getZjName());
            mateTemplateModel.setReportAuditName(productReport.getAuditName());
            mateTemplateModel.setReportQualityName(productReport.getQualityName());
            mateTemplate.setCheckNum(productReport.getCheckNum());
            mateTemplateModel.setCheckCategory(productReport.getCheckCategory());
            mateTemplateModel.setProductGrade(productReport.getProductGrade());
            mateTemplateModel.setCheckAccording("《" + productReport.getProductName() + "》" + "技术要求");
            mateTemplateModel.setReportInspection(productReport.getCheckResult());

            if (productReport.getReportOrderNum() != null) {
                mateTemplateModel.setReportOrderNum(codeAutoIncrement(productReport.getReportOrderNum()));
            }

            mateTemplate.setUnit(finishedProductModel.getUnit());
            mateTemplate.setWorkshopName(finishedProductModel.getWorkshopName());
            mateTemplate.setAcceptName(finishedProductModel.getAcceptName());
            mateTemplate.setWarehousingName(finishedProductModel.getWarehousingName());

            mateTemplate.setMateAsepsis(mateAsepsis);
            mateTemplate.setMateEthylene(mateEthylene);

            resMapa.put("mateTemplate", mateTemplate);
        } else {
            resMapa.put("mateTemplate", "");
        }

        return resMapa;
    }


}

